package com.bw.exam.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bw.exam.pojo.Brand;
import com.bw.exam.result.R;
import com.bw.exam.service.BrandService;
import com.bw.exam.util.FreemarkerUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author: liuzhiping
 * @date: 2024/8/15 11:27
 **/
@RequestMapping("/manage/brand")
@RestController
@Slf4j
public class BrandController {

    @Autowired
    private BrandService brandService;

    @RequestMapping("/list")
    public R page(@RequestBody Brand brand){

        log.info("brand 查询 brand:{}",JSON.toJSONString(brand));
        //查询、分页
        LambdaQueryWrapper<Brand> lambdaQueryWrapper = new LambdaQueryWrapper<Brand>();
        if(StringUtils.isNotBlank(brand.getName())) {
            lambdaQueryWrapper.eq(Brand::getName, brand.getName());
        }
        if(StringUtils.isNotBlank(brand.getFirstChar())) {
            lambdaQueryWrapper.eq(Brand::getFirstChar, brand.getFirstChar());
        }

        Page<Brand> page = new Page<Brand>(brand.getStartPage(),brand.getEndPage());

        Page resultPage = this.brandService.page(page,lambdaQueryWrapper);

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("list",resultPage.getRecords());
        map.put("total",resultPage.getTotal());

        return R.success(map);

    }




    @RequestMapping("/list/all")
    public R listall(){

        log.info("brand 查询 list all");

        return R.success(this.brandService.list());

    }
    @RequestMapping("/add")
    public R add(@RequestBody Brand brand){

        log.info("brand 添加 brand:{}",JSON.toJSONString(brand));
        this.brandService.save(brand);

        return R.success();

    }

    @RequestMapping("/update")
    public R update(@RequestBody Brand brand){

        log.info("brand 添加 brand:{}",JSON.toJSONString(brand));
        this.brandService.updateById(brand);
        return R.success();

    }

    @RequestMapping("/delIds")
    public R update(@RequestBody Long[] ids){

        log.info("brand 删除:{}",JSON.toJSONString(ids));
        this.brandService.removeByIds(Arrays.asList(ids));
        return R.success();

    }

    @RequestMapping("/freemarker")
    public R freemarker() throws Exception{

        log.info("brand freemarker");
        List<Brand> list = this.brandService.list();
        FreemarkerUtil freemarkerUtil = new FreemarkerUtil();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("list",list);
        String outputfile = "/Users/liuzhiping/datafile/"+ UUID.randomUUID().toString()+".html";
        freemarkerUtil.createHtml(map,"brand",outputfile);
        return R.success();

    }




}
